package de.piratech.dasding.data.database.couch;

import java.util.ArrayList;
import java.util.List;

import org.ektorp.CouchDbConnector;
import org.ektorp.support.CouchDbRepositorySupport;

import de.piratech.dasding.data.Action;
import de.piratech.dasding.data.database.ActionData;
import de.piratech.dasding.data.database.couch.adapters.ActionAdapter;

public class ActionDataImpl extends CouchDbRepositorySupport<ActionAdapter>
		implements ActionData {

	protected ActionDataImpl(CouchDbConnector db) {
		super(ActionAdapter.class, db, "Action");
	}

	@Override
	public Action createAction(Action action) {
		if (action instanceof ActionAdapter) {
			add((ActionAdapter) action);
		} else {
			add(new ActionAdapter(action));
		}
		return action;
	}

	@Override
	public void deleteAction(String id) {
		remove(get(id));
	}

	@Override
	public Action updateAction(Action action) {
		if (action instanceof ActionAdapter) {
			update((ActionAdapter) action);
		} else {
			update(new ActionAdapter(action));
		}
		return action;
	}

	@Override
	public Action getAction(String id) {
		return get(id);
	}

	@Override
	public List<Action> getActions(String eventId) {
		return new ArrayList<Action>(queryView("byEvent", eventId));
	}

	@Override
	public List<Action> getActions() {
		return new ArrayList<Action>(getAll());
	}
}
